//
//  StatusViewModel.swift
//  Practice
//
//  Created by miaozhihui on 2017/2/9.
//  Copyright © 2017年 DeKuTree. All rights reserved.
//

import Foundation

/// 微博视图模型 如果没有任何父类，需要遵守CustomStringConvertible 实现description 计算型属性
class StatusViewModel: CustomStringConvertible {
    
    /// 微博模型
    var status: Status
    
    /// 会员图标
    var memberIcon: UIImage?
    
    /// 认证类型:-1 没有认证 0 认证用户 1、2、3、5 企业认证 220 达人
    var vipIcon: UIImage?
    
    /// 转发数
    var repostsString: String?
    
    /// 评论数
    var commentsString: String?
    
    /// 点赞数
    var attitudesString: String?
    
    /// 配图视图大小
    var pictureViewSize = CGSize()
    
    /// 显示配图信息
    var picURLs: [StatusPicture]? {
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    /// 微博正文的属性文字
    var statusAttrText: NSAttributedString?
    
    /// 转发文字的属性文本
    var retweetedAttrText: NSAttributedString?
    
    /// 行高
    var rowHeight: CGFloat = 0
    
    
    
    /// 构造函数
    ///
    /// - Parameter model: 微博模型
    init(model: Status) {
        status = model
        guard let mbrank = model.user?.mbrank else {
            return
        }
        if mbrank > 0 && mbrank < 7 {
            memberIcon = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 1,2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        repostsString = countString(count: model.reposts_count, defaultString: "转发")
        commentsString = countString(count: model.comments_count, defaultString: "评论")
        attitudesString = countString(count: model.attitudes_count, defaultString: "赞")
        // 配图视图大小
        pictureViewSize = countPictureViewSize(count: picURLs?.count)
        // 微博正文的属性文本
        statusAttrText = EmoticonManager.shared.emoticonString(string: model.text ?? "", font: UIFont.systemFont(ofSize:15))
        // 被转发微博的文字
        let rText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        retweetedAttrText = EmoticonManager.shared.emoticonString(string:rText,font:UIFont.systemFont(ofSize:14))
        // 计算行高
        updateRowHeight()
    }
    
    var description: String {
        return status.description
    }
    
    func updateRowHeight() {
        let margin: CGFloat = 12
        let iconHeight: CGFloat = 34
        let tooBarHeight: CGFloat = 35
        var height: CGFloat = 0
        let viewSize = CGSize(width: UIScreen.xs_screenWidth()-2*margin, height: CGFloat(MAXFLOAT))
        height = 2*margin+iconHeight+margin
        if let text = statusAttrText {
            height += text.boundingRect(with: viewSize, options: .usesLineFragmentOrigin, context: nil).height
        }
        if status.retweeted_status != nil {
            height += 2*margin
            if let text = retweetedAttrText {
                height += text.boundingRect(with: viewSize, options: .usesLineFragmentOrigin, context: nil).height
            }
        }
        height += pictureViewSize.height
        height += margin
        height += tooBarHeight
        rowHeight = height
    }
    
    /// 使用单个图像更新配图视图的大小
    ///
    /// - Parameter image: 网络缓存的单张图像
    func updateSingleImageSize(image: UIImage) {
        
        var size = image.size
        
        let maxWidth: CGFloat = 300
        let minWidth: CGFloat = 40
        
         // 过宽图像处理
        if size.width > maxWidth {
            size.width = maxWidth
            size.height = size.width * image.size.height / image.size.width
        }
        // 过窄图像处理
        if size.width < minWidth {
            size.width = minWidth
            size.height = size.width * image.size.height / image.size.width
        }
        
        size.height += StatusPictureViewOutterMargin
        
        pictureViewSize = size
        
    }
    
    /// 计算指定数量的图片对应的配图视图的大小
    ///
    /// - Parameter count: 配图数量
    /// - Returns: 配图视图的大小
    private func countPictureViewSize(count: Int?) -> CGSize {
        
        guard let count = count else { return CGSize() }
        if count == 0 {
            return CGSize()
        }
        // 微博配图视图的高度
        let row = (count - 1) / 3 + 1
        let StatusPictureViewHeight: CGFloat = StatusPictureViewOutterMargin + StatusPictureItemWidth * CGFloat(row) + CGFloat(row - 1) * StatusPictureViewInnerMargin
        return CGSize(width: StatusPictureViewWidth, height: StatusPictureViewHeight)
    }
    
    /// 给定一个数字，返回对应的描述结果
    ///
    /// - Parameters:
    ///   - count: 数量
    ///   - defaultString: 默认文本
    /// - Returns: 描述结果
    /*
     count == 0，显示默认标题
     count > 10000，显示 x.xx 万
     count < 10000，显示实际数字
     */
    private func countString(count: Int, defaultString: String) -> String {
        if count == 0 {
            return defaultString
        }
        if count < 10000 {
            return count.description
        }
        return String(format: "%.02f 万", Double(count) / 10000)
    }
}
